package View;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import java.awt.BorderLayout;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;

public class ServerView implements ViewInterface{

	private JTextPane text;
	private JScrollPane scrollPane;
	private boolean pause = false;
	
	private static ServerView view;
	
	public static ServerView getServerView(){
		return view == null ? view = new ServerView() : view;
	}
	
	/**
	 * Create the frame.
	 */
	private ServerView() {
		try {
			UIManager.setLookAndFeel(UIManager.getInstalledLookAndFeels()[3].getClassName());
		} catch (Exception e){
			e.printStackTrace();
		}
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0,0, 1024,768);
		frame.setResizable(false);
		
		scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		text = new JTextPane();
		text.setFont(new Font("Rockwell", Font.PLAIN, 20));
		text.setBackground(Color.BLACK);
		text.setForeground(Color.GREEN);
		text.setText("server>");
		text.setEditable(false);
		scrollPane.setViewportView(text);
		frame.setVisible(true);
		
		frame.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {}
			
			@Override
			public void keyReleased(KeyEvent e) {}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_SPACE)
					pause = !pause;
			}
		});
		frame.requestFocus();
	}

	@Override
	public void showOperation(String operation) {
		Date d = new Date(System.currentTimeMillis());
		text.setText(text.getText() + operation +"     time:"+d.toString() +"\nserver>");
		if(!pause)
			scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
	}

}
